package com.dqp.cslggroup.MenuViewPage;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;

import com.dqp.cslggroup.EventBus.MessageEvent;
import com.dqp.cslggroup.R;
import com.dqp.cslggroup.Util.Comment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

public class menu_Fragment3 extends Fragment {
    private View view;
    private Context context;
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.notification, null);
        context = view.getContext();
        EventBus.getDefault().register(this);
        return view;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (view != null && isVisibleToUser) {
            getPush();
        }
    }

    private void getPush() {
//        new Thread(() -> {
//            ArrayList<Comment> dataList = Push.getAllPush();
//            if (dataList.size() > 0) {
//                EventBus.getDefault().post(new MessageEvent("menu_Fragment3", 1, dataList));
//            } else {
//                EventBus.getDefault().post(new MessageEvent("menu_Fragment3", 2, null));
//            }
//        }).start();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(MessageEvent event) {
        if (event.getEvent().equals("menu_Fragment3")) {
            if (event.getWhat() == 1) {
                ArrayList<Comment> dataList = (ArrayList<Comment>) event.getObj();
                Comment comment = dataList.get(0);
                TextView name = view.findViewById(R.id.comment_name);
                ImageView imageView = view.findViewById(R.id.comment_head);
                TextView content = view.findViewById(R.id.comment_content);
                TextView date = view.findViewById(R.id.comment_date);
                TextView college = view.findViewById(R.id.comment_college);
                date.setText(comment.getDate());
                college.setText(comment.getCollege());
                name.setText(comment.getComment_name());
                content.setText(comment.getContent());
                Drawable drawable = ContextCompat.getDrawable(context, R.drawable.stranger);
                imageView.setBackground(drawable);
            }
        }
    }

}
